package com.yunfei.schedule;
import com.yunfei.api.notice.result.MessageNoticeReturnBo;
import com.yunfei.api.notice.result.MessageNoticeSendTimeBo;
import com.yunfei.entity.messageSendDtl.MessageSendDtlEntity;
import com.yunfei.mapper.aiiidb.DaoZhangQueRenMessageForLiCaiShiMapper;
import com.yunfei.mapper.aiiidb.MessageNoticeMapper;
import com.yunfei.utils.ComponentFactory;
import com.yunfei.utils.DateUtil;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description: 到处理时间更新需要处理的数据
* @Author: zhengyunfei
* @Date: 2019/3/21
* @time:11:03
*/
@Component
public class UpdateMessageTypeTask {
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(UpdateMessageTypeTask.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

   //@Scheduled(cron = "0 0 1 * * ?")   //每天凌晨1点执行
    @Scheduled(cron = "0 0/1 * * * ?")   //1分钟执行一次
   /*@Scheduled(fixedRate = 50000000)*/
    public void testMyBatis() {
        System.out.println("到处理时间更新需要处理的消息数据，定时任务。。。。。。。。");
        try {
            MessageNoticeMapper messageNoticeMapper = ComponentFactory.getBean(MessageNoticeMapper.class);
            Map<String,Object> queryMap=new HashMap<>();
            List<MessageNoticeSendTimeBo> list=messageNoticeMapper.getScheduleTime(queryMap);
            for(int i=0;i<list.size();i++){
                String sendDate=list.get(i).getSendDate();
                //判断发送时间和当前定时任务执行的时间间隔是否一致，若一致，则执行按时间处理更新
                String dateNow=messageNoticeMapper.getDateNow();
              //  DateUtil.
                long time=DateUtil.getBetween(dateNow, sendDate, DateUtil.YYYYMMDDHHMM, DateUtil.MINUTE_RETURN);
                System.out.println("需要执行批处理的时间为==========="+sendDate+",距离执行时间还有 "+time+"分钟");
                Map<String,Object> updateMap=new HashMap<>();
                updateMap.put("sendDate",sendDate);
                if(time==0){
                    System.out.println(sendDate+"的批处理开始执行==========="+sendDate);
                    messageNoticeMapper.updateMessageType(updateMap);
                    System.out.println(sendDate+"的批处理开始完毕===========执行完毕的时间="+DateUtil.getDateNow("yyyy-MM-dd HH:mm:ss"));
                }
            }
            //更新过期未发送的消息耿直
            messageNoticeMapper.updateNotSendMessage();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
}

